<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改游戏附件')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-file-edit" th:object="${bizFile}">
        <input id="id" name="id" th:field="*{id}" type="hidden">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">游戏id：</label>
                <div class="col-sm-8">
                    <select class="form-control noselect2 selectpicker" id="gameId" name="gameId">
                        <option th:each="game : ${games}" th:value="${game.id}" th:text="${game.name}">
                            --请选择游戏--
                        </option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">附件名称：</label>
                <div class="col-sm-8">
                    <input id="name" name="name" th:field="*{name}" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">附件链接：</label>
                <div class="col-sm-8">
                    <input id="link" name="link" th:field="*{link}" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">附件预览：</label>
                <div class="col-sm-8" th:if="${bizFile.type == '1'}">

                    <img th:src="${bizFile.link}" th:alt="${bizFile.link}" class="form-control"
                         th:if="${#strings.contains(bizFile.link, 'http')}"/>

                    <img th:src="@{${bizFile.link}}" th:alt="@{${bizFile.link}}" class="form-control"
                         th:unless="${#strings.contains(bizFile.link, 'http')}"/>
                </div>
                <div class="col-sm-8" th:if="${bizFile.type == '0'}">
                    <video width="320" height="240" controls>
                        <source th:src="@{${bizFile.link}}" type="video/mp4"
                                th:unless="${#strings.contains(bizFile.link, 'http')}">
                        <source th:src="${bizFile.link}" type="video/mp4"
                                th:if="${#strings.contains(bizFile.link, 'http')}">
                    </video>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">附件类型：</label>
                <div class="col-sm-8">
                    <select id="type" name="type" class="form-control" th:with="type=${@dict.getType('biz_file_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                                th:field="*{type}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">是否为封面：</label>
                <div class="col-sm-8">
                    <select id="face" name="face" class="form-control" th:with="type=${@dict.getType('biz_file_face')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                                th:field="*{type}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea id="remark" name="remark" class="form-control">[[*{remark}]]</textarea>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">上传：</label>
                <div class="col-sm-8">
                    <input id="filePath" name="filePath" class="form-control" type="file">
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "biz/file";
    $("#form-file-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            // $.operate.save(prefix + "/edit", $('#form-file-edit').serialize());
            uploadFile();
        }
    }

    function uploadFile() {
        var formData = new FormData();
        if ($('#filePath')[0].files[0] == null) {
            // $.modal.alertWarning("请先选择文件路径");
            // return false;
            // 没有选文件走普通提交
            console.log(("------------- 没有选择文件 --------------"))
            // if ($.validate.form()) {
            //     $.operate.save(prefix + "/edit", $('#form-file-add').serialize());
            // }
        } else {
            formData.append('file', $('#filePath')[0].files[0]);
        }

        // 其他表单内容
        formData.append('gameId', $("#gameId").val());
        formData.append('link', $("#link").val());
        formData.append('name', $("#name").val())
        formData.append('type', $("#type").val());
        formData.append('face', $("#face").val());
        formData.append('remark', $("#remark").val());
        formData.append('id', $("#id").val());

        $.ajax({
            url: prefix + "/edit",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                $.operate.successCallback(result);
            }
        });

    }
</script>
</body>
</html>